/*
 * Copyright [2013-2021], Alibaba Group Holding Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.alibaba.polardbx.executor.ddl.job.validator;

import com.alibaba.polardbx.common.exception.TddlRuntimeException;
import com.alibaba.polardbx.common.exception.code.ErrorCode;
import com.alibaba.polardbx.common.properties.ParamManager;
import com.alibaba.polardbx.common.utils.GeneralUtil;
import com.alibaba.polardbx.gms.metadb.limit.LimitValidator;
import com.alibaba.polardbx.gms.tablegroup.TableGroupConfig;
import com.alibaba.polardbx.optimizer.OptimizerContext;

public class TableGroupValidator {

    public static void validateTableGroupInfo(String schemaName, String tableGroupName, boolean isAllowEmpty,
                                              ParamManager paramManager) {
        validateTableGroupNameLength(tableGroupName);

        checkIfTableGroupExists(schemaName, tableGroupName, isAllowEmpty);

        validateTableGroupExistence(schemaName, tableGroupName);

        LimitValidator.validateTableCount(schemaName);

        // Check the number of table partitions per physical database.
        validateTableGroupPartitionNum(schemaName, tableGroupName, paramManager);
    }

    public static void validateTableGroupNameLength(String tableGrupName) {
        LimitValidator.validateTableGroupNameLength(tableGrupName);
    }

    public static void validateTableGroupPartitionNum(String schemaName, String tableGroupName,
                                                      ParamManager paramManager) {
        TableGroupConfig tableGroupConfig = OptimizerContext.getContext(schemaName).getTableGroupInfoManager()
            .getTableGroupConfigByName(tableGroupName);
        if (tableGroupConfig != null && GeneralUtil.isNotEmpty(tableGroupConfig.getPartitionGroupRecords())) {
            LimitValidator.validateTablePartitionNum(tableGroupConfig.getPartitionGroupRecords().size(), paramManager);
        }
    }

    public static void validateTableGroupExistence(String schemaName, String tableGroupName) {
        TableGroupConfig tableGroupConfig = OptimizerContext.getContext(schemaName).getTableGroupInfoManager()
            .getTableGroupConfigByName(tableGroupName);
        if (tableGroupConfig == null) {
            throw new TddlRuntimeException(ErrorCode.ERR_TABLE_GROUP_NOT_EXISTS, tableGroupName);
        }
    }

    public static void checkIfTableGroupExists(String schemaName, String tableGroupName, boolean isAllowEmpty) {
        validateTableGroupExistence(schemaName, tableGroupName);
        TableGroupConfig tableGroupConfig = OptimizerContext.getContext(schemaName).getTableGroupInfoManager()
            .getTableGroupConfigByName(tableGroupName);
        if (tableGroupConfig.getTableCount() == 0 && !isAllowEmpty) {
            throw new TddlRuntimeException(ErrorCode.ERR_PARTITION_MANAGEMENT,
                tableGroupName + " when it's empty");
        }
    }

}
